package com.alax.interfaces.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alax.common.core.domain.AjaxResult;
import com.alax.interfaces.domain.ApiCallRequest;
import com.alax.interfaces.service.IApiCallService;
import com.alax.common.annotation.Log;
import com.alax.common.enums.BusinessType;
import com.alax.common.core.controller.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * API调用Controller
 * 
 * @author alax
 */
@Api(tags = "API调用接口")
@RestController
@RequestMapping("/interfaces/call")
public class ApiCallController extends BaseController {
    
    private static final Logger log = LoggerFactory.getLogger(ApiCallController.class);
    
    @Autowired
    private IApiCallService apiCallService;
    
    /**
     * 调用API接口
     * 
     * @param request API调用请求
     * @return 调用结果
     */
    @ApiOperation(value = "调用API", notes = "根据API ID调用指定的接口")
    @PostMapping
    @Log(title = "API调用", businessType = BusinessType.OTHER)
    public AjaxResult call(@ApiParam(value = "API调用请求", required = true) @Valid @RequestBody ApiCallRequest request) {
        log.info("接收到API调用请求: {}", request);
        
        try {
            // 调用服务
            AjaxResult result = apiCallService.callApi(request);
            log.info("API调用完成: {}", result);
            return result;
        } catch (Exception e) {
            log.error("API调用异常: ", e);
            return AjaxResult.error(e.getMessage());
        }
    }
}
